package com.michaelwolf.mytool.csenvsimulator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class CustomerDataLoader { 
	
	private HashMap<String, Map<String, QueryRecord>> queryResultCache = new HashMap<>();
	
	public Set<String> getQueryCommandList(){
		return queryResultCache.keySet();
	}
	
	public Set<String> getQueryCommandTimestampSet(String commandKey){
		Map<String, QueryRecord> map = queryResultCache.get(commandKey);
		if(map!=null){
			return map.keySet();
		}
		else{
			return new HashSet<String>();
		}
	}
	
	public QueryRecord getQueryCmmandRecord(String commandKey, String commandTimestampKey){
		Map<String, QueryRecord> map = queryResultCache.get(commandKey);
		if(map!=null){
			return map.get(commandTimestampKey);
		}
		
		return null;
	}
	
	public void loadRecordFile(File recordFile) throws IOException{
		BufferedReader reader = new BufferedReader(new FileReader(recordFile));
		
		String str;
		String targetUID = "", ipAddress = "", command = "", commandOutput = "", commandTimestamp = "", commandDuration = "", errorOutput = "", errorTimestamp = "";
		StringBuilder strbuilder = new StringBuilder();
		String content;
		while((str = reader.readLine())!=null){
			if(str.contains("\"targetUID\"")){
				content = str.substring(str.indexOf("\"targetUID\": ") + 13, str.length());
				strbuilder = new StringBuilder();
				strbuilder.append(content);
			}else if(str.contains("\"collectionID\"")){
				targetUID = strbuilder.substring(0, strbuilder.length()-1);
			}else if(str.contains("\"ipAddress\"")){
				strbuilder = new StringBuilder();
				strbuilder.append(str.substring(str.indexOf("\"ipAddress\":") + 14, str.length()));
			}else if(str.contains("\"command\"")){
				ipAddress = strbuilder.substring(0, strbuilder.length()-2);
				strbuilder = new StringBuilder();
				strbuilder.append(str.substring(str.indexOf("\"command\":") + 12, str.length()));
			}else if(str.contains("\"commandOutput\"")){
				command = strbuilder.substring(0, strbuilder.length()-2);
				strbuilder = new StringBuilder();
				strbuilder.append(str.substring(str.indexOf("\"commandOutput\":") + 18, str.length()));
			}else if(str.contains("\"commandTimestamp\"")){
				commandOutput = strbuilder.substring(0, strbuilder.length()-2);
				strbuilder = new StringBuilder();
				strbuilder.append(str.substring(str.indexOf("\"commandTimestamp\":") + 21, str.length()));
			}else if(str.contains("\"commandDuration\"")){
				commandTimestamp = strbuilder.substring(0, strbuilder.length()-2);
				strbuilder = new StringBuilder();
				strbuilder.append(str.substring(str.indexOf("\"commandDuration\":") + 19, str.length()));
			}else if(str.contains("\"errorOutput\"")){
				commandDuration = strbuilder.substring(0, strbuilder.length()-1);
				strbuilder = new StringBuilder();
				strbuilder.append(str.substring(str.indexOf("\"errorOutput\":") + 16, str.length()));
			}else if(str.contains("\"errorTimestamp\"")){
				errorOutput = strbuilder.substring(0, strbuilder.length()-2);
				strbuilder = new StringBuilder();
				strbuilder.append(str.substring(str.indexOf("\"errorTimestamp\":") + 19, str.length()));
			}else if(str.contains("}")){
				if(strbuilder.length()<=2){
					System.out.println(strbuilder.length());
				}
				
				errorTimestamp = strbuilder.substring(0, strbuilder.length()-2);
				
				addQueryRecord(targetUID, ipAddress, command, commandOutput, commandTimestamp, commandDuration, errorOutput, errorTimestamp);
			}else{
				strbuilder.append(str);
			}
			
		}
		reader.close();
	}
	
	private void addQueryRecord(String targetUID, String ipAddress,
			String command, String commandOutput, String commandTimestamp,
			String commandDuration, String errorOutput, String errorTimestamp) {
		QueryRecord record = new QueryRecord(targetUID, ipAddress, command, commandOutput,
				commandTimestamp, commandDuration, errorOutput, errorTimestamp);
		
		Map<String, QueryRecord> specificCommandMap = queryResultCache.get(command);
		if(specificCommandMap == null){
			specificCommandMap = new HashMap<>();
			queryResultCache.put(command, specificCommandMap);
		}
		specificCommandMap.put(commandTimestamp, record);
		
	}

	class QueryRecord {
		private final String targetUID;
		
		private final String ipAddress;
		
		private final String command;
		
		private final String commandOutput;
		
		private final String commandTimestamp;
		
		private final String commandDuration;
		
		private final String errorOutput;
		
		private final String errorTimestamp;
		
		public QueryRecord(String targetUID, String ipAddress, String command,
				String commandOutput, String commandTimestamp,
				String commandDuration, String errorOutput,
				String errorTimestamp) {
			super();
			this.targetUID = targetUID;
			this.ipAddress = ipAddress;
			this.command = command;
			this.commandOutput = commandOutput;
			this.commandTimestamp = commandTimestamp;
			this.commandDuration = commandDuration;
			this.errorOutput = errorOutput;
			this.errorTimestamp = errorTimestamp;
		}

		public String getTargetUID() {
			return targetUID;
		}

		public String getIpAddress() {
			return ipAddress;
		}

		public String getCommand() {
			return command;
		}

		public String getCommandOutput() {
			return commandOutput;
		}

		public String getCommandTimestamp() {
			return commandTimestamp;
		}

		public String getCommandDuration() {
			return commandDuration;
		}

		public String getErrorOutput() {
			return errorOutput;
		}

		public String getErrorTimestamp() {
			return errorTimestamp;
		}
		
		
	}
}
